import { ApiCall } from "tsrpc";
import { ReqLogined, ResLogined } from "../../../tsrpc/shared/protocols/account/PtlLogined";
import { as } from "../../../AccountServiceEntra";
import { User } from "../../../module/common/User";

export default async function (call: ApiCall<ReqLogined, ResLogined>) {

    var dUser = await User.getUserByAccount(call.req.account);
    if (dUser) {
        if (dUser.password != call.req.password) {
            call.error(`用户名或密码错误`, { code: 'Account_Or_Password_Error' });
            return;
        }
        
        call.logger.log(`账号服务器[${call.req.account}]已登录`);
        as.AccountModel.users.set(dUser.key, dUser);
        var sso = await as.createSsoToken(dUser.key);
        call.succ({ __ssoToken: sso ,uid:dUser.key});
    }
    else {
        call.error(`账号[${call.req.account}]不存在`, { code: 'Exist_No_Account' });
    }
    

    
}